package com.chen.manager.service.impl;

import com.chen.manager.helper.SysMenuHelper;
import com.chen.manager.mapper.SysMenuMapper;
import com.chen.manager.service.SysMenuService;
import com.chen.model.entity.system.SysMenu;
import com.chen.model.entity.system.SysUser;
import com.chen.model.vo.common.ResultCodeEnum;
import com.chen.model.vo.system.SysMenuVo;
import com.chen.service.exception.MyException;
import com.chen.util.AuthContextUtil;
import org.ehcache.core.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cgj
 * @date 2/22/25 4:28 PM
 */
@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    @Transactional(readOnly = true)
    public List<SysMenu> showAll() {
//       直接查询出来的是不带层次结构的, 需要手动编写
        List<SysMenu> sysMenuList=sysMenuMapper.selectAll();
        if(CollectionUtils.isEmpty(sysMenuList)){
            return  null;
        }
        List<SysMenu> SysMenuTree= SysMenuHelper.generateTree(sysMenuList);
        return SysMenuTree;
    }

    @Override
    public void add(SysMenu sysMenu) {
//        //添加父节点
//        sysMenu.setParentId(0L);
//        sysMenu.setStatus(1); //1启用
        sysMenuMapper.insertSysMenu(sysMenu);
    }

    @Override
    public void update(SysMenu sysMenu) {
            sysMenuMapper.updateSysMenu(sysMenu);
    }

    @Override
    public void removeSysMenuById(Long id) {
        // 判断id下是否有子菜单
//         SysMenu sysMenu=sysMenuMapper.selectById(id);
//         if(!CollectionUtils.isEmpty(sysMenu.getChildren())){
//             throw new MyException(ResultCodeEnum.NODE_ERROR);
//         }

          Long count=sysMenuMapper.selectSubCount(id);
          if(count>0){
              System.out.println(11);
              throw new MyException(ResultCodeEnum.NODE_ERROR);
          }

         // 没有子菜单开始删除
         sysMenuMapper.deleteById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<SysMenuVo> getMenus() {
        //在threadLocal中获取用户对象
        SysUser sysUser = AuthContextUtil.get();
        //不用判断sysUser是否为空,如果为null,就根本执行不到这个方法
        //根据用户的id,查询用户所拥有的菜单
        List<SysMenu> sysMenuList=sysMenuMapper.selectSysMenuByUserId(sysUser.getId());
        //创建树形结构
        List<SysMenu> sysMenuListTree = SysMenuHelper.generateTree(sysMenuList);
        if(CollectionUtils.isEmpty(sysMenuListTree)) return  null;
        List<SysMenuVo> sysMenuVoList = buildMenus(sysMenuListTree);

        return sysMenuVoList;
    }

    private static List<SysMenuVo> buildMenus(List<SysMenu> sysMenuListTree) {
        List<SysMenuVo> sysMenuVoList=new ArrayList<>();
        //开始构建返回的vo对象
        for (SysMenu sysMenu : sysMenuListTree) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if(!CollectionUtils.isEmpty(children)){
                sysMenuVo.setChildren(buildMenus(children));
            }
           sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
